package com.onesports.intelligent.k12.polarlight.service.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.config.ApplicationProperties;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.IdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.InsertBannerDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.UpdateBannerDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.BannerConfig;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.BannerConfigVO;
import com.onesports.intelligent.k12.polarlight.enums.BannerTypeEnum;
import com.onesports.intelligent.k12.polarlight.mapper.sys.BannerConfigMapper;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.util.BeanCopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轮播图Service
 *
 * @author wangcx
 **/
@Service
@RequiredArgsConstructor
public class BannerService extends ServiceImpl<BannerConfigMapper, BannerConfig> {


    private final BannerConfigMapper mapper;
    private final ApplicationProperties properties;

    /**
     * 新增轮播图
     *
     * @param dto 入参
     */
    public void add(InsertBannerDTO dto) {
        checkSort(dto,null);
        String orgId = SecurityUtils.getOrganizationId();
        dto.setOrganizationId(orgId);
        dto.setBannerType(BannerTypeEnum.BANNER_TYPE.getValue());
        BannerConfig entity = new BannerConfig();
        BeanCopyUtil.copyProperties(dto, entity);
        entity.setAppid(ParamUtils.isNotEmpty(dto.getAppid())?dto.getAppid():properties.getWx().getMiniapp().getAppid());
        mapper.insert(entity);
    }
    /**
     * function: 排序号判断
     *
     * @param dto 轮播图DTO
     * @author WangCX
     * @since 2022/8/3 16:33
     */
    private void checkSort(InsertBannerDTO dto,String id) {
        Long aLong = this.mapper.selectCount(new LambdaQueryWrapper<BannerConfig>()
                .ne(ParamUtils.isNotEmpty(id), BannerConfig::getId, id)
                .eq(BannerConfig::getSort, dto.getSort()));
        if (aLong > 0){
            throw new BusinessException("排序号已存在！");
        }

    }

    /**
     * 修改轮播图
     *
     * @param dto 入参
     */
    public void upd(UpdateBannerDTO dto) {
        checkSort(dto,dto.getId());
        String orgId = SecurityUtils.getOrganizationId();
        dto.setOrganizationId(orgId);
        dto.setBannerType(BannerTypeEnum.BANNER_TYPE.getValue());
        BannerConfig entity = new BannerConfig();
        BeanCopyUtil.copyProperties(dto, entity);
        mapper.updateById(entity);

    }

    /**
     * 删除轮播图
     *
     * @param dto 入参
     */
    public void del(IdDTO dto) {
        mapper.deleteById(dto.getId());
    }

    /**
     * function: 获取全部轮播图
     *
     * @param page 分页参数
     * @param orgId 机构id
     * @return BannerConfig 轮播图实体类
     * @author WangCX
     * @since 2022/8/1 15:00
     */
    public IPage<BannerConfig> findPage(Page<BannerConfig> page, String orgId) {
        return this.mapper.findPage(page, orgId);
    }

    /**
     * 小程序端首页获取轮播图
     *
     * @return BannerConfigVO
     * @author WangCX
     */
    public List<BannerConfigVO> getBanner() {
        List<BannerConfig> bannerConfigs = this.mapper.selectList(new LambdaQueryWrapper<BannerConfig>()
                .eq(BannerConfig::getDeleted, false)
                .eq(BannerConfig::getStatus, true)
                .eq(BannerConfig::getBannerType, BannerTypeEnum.BANNER_TYPE.getValue()));
        return BeanCopyUtil.copyListProperties(bannerConfigs,BannerConfigVO::new);
    }
}
